/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.Dao.UserImpl;
import com.lcfgly.Dao.UserconuntImpl;
import com.lcfgly.Database.UserConunt;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.SQLEmptyException;
import com.lcfgly.util.ConfigDate;
import com.lcfgly.util.Encode;
import java.io.Serializable;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author lcfgly
 */
@Service
public class LoginService implements Serializable{
    
    Logger loginlog=Logger.getLogger("Login");
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    UserImpl userImpl;
    
    @Autowired
    UserconuntImpl userconuntImpl;
    
    /**
     * 用户名检测
     * @param username
     * @return 
     * @throws com.lcfgly.Exception.ActionException 
     */
    public boolean checkUsername(String username) throws ActionException
    {
        try{
            userImpl.usernameCheck(username);
            return false;
        }catch(SQLEmptyException e){
            return true;
        }catch(SQLException e){
            log.error(e);
            throw new ActionException("用户名不可用！");
        }
    }
    
    /**
     * 用户登入
     * @param username
     * @param password
     * @return 
     * @throws com.lcfgly.Exception.ActionException 
     */
    public UserInfo userLogin(String username,String password) throws ActionException
    {
        try {
            UserInfo user=userImpl.findShopbyusername(username);
            if(!user.getStart())
                throw new ActionException("账户尚未开通！请查看开通流程:<a href='"+ConfigDate.config("openuserurl")+"' target='_blank'>查看</a>");
            if(user.getPassword() == null)
                throw new ActionException("数据错误！");
            String pad=Encode.passwordcode(password, user.getSolt());
            if(pad == null ?false: pad.equals(user.getPassword())){
                loginlog.info(username+" 登入系统");
                return user;
            }
            else
                throw new ActionException("用户密码错误！");
        } catch (SQLException ex) {
            log.error(ex);
           throw new ActionException("用户不存在或其他未知错误！");
        }
    }
    
    /**
     * 用户注册
     * @param users
     * @return
     * @throws com.lcfgly.Exception.ActionException
     */
    public UserInfo userRegister(UserInfo users) throws ActionException
    {
        if(users==null){
            throw new ActionException("空数据！ ");
        }else if(users.getUsername() == null){
            throw new ActionException("用户名不能为空！");
        }else if(!checkUsername(users.getUsername())){
            throw new ActionException("用户名已被使用！");
        }else if(users.getPassword() == null || users.getPassword().length()<6){
            throw new ActionException("密码不符合要求：不能为空且长度不能小于6 ！");
        }else
        {
            if(users.getUname() == null)
                users.setUname(users.getUsername());
            users.setSolt(Encode.randomString(4));
            users.setPassword(Encode.passwordcode(users.getPassword(), users.getSolt()));
            users.setStart(false);
            users.setRole((short)0);
            try {
                int x=userImpl.insertBackID(users);
                if(x>0)
                {
                    users.setUid(x);
                    userconuntImpl.insert(new UserConunt(x));
                    return users;
                }
                else
                    throw new ActionException("用户注册失败!");
            } catch (SQLException ex) {
                log.error(ex);
                throw new ActionException("用户注册失败！请联系管理员");
            }
        }
    }
    
}
